package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.SysStoreClientsEntity;
import com.example.mybatis.service.impl.CrudServiceImpl;
import com.example.dao.SysStoreDao;
import com.example.dto.SysStoreDTO;
import com.example.entity.SysStoreEntity;
import com.example.security.InfoDetail;
import com.example.security.SecurityUser;
import com.example.service.Oauth2RegisteredClientService;
import com.example.service.SysStoreService;
import com.example.utils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 租户门店表
 *
 * @author yyyyjinying 1020066395@qq.com
 * @since 1.0-SNAPSHOT 2025-04-19
 */
@Service
public class SysStoreServiceImpl extends CrudServiceImpl<SysStoreDao, SysStoreEntity, SysStoreDTO> implements SysStoreService {

    @Autowired
    private Oauth2RegisteredClientService oauth2RegisteredClientService;

    @Override
    public QueryWrapper<SysStoreEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<SysStoreEntity> wrapper = new QueryWrapper<>();

        String storeName = (String)params.get("storeName");
        wrapper.eq(StringUtils.isNotBlank(storeName), "store_name", storeName);

        InfoDetail user = SecurityUser.getUser();
        Integer isSuperTenant = user.getIsSuperTenant();
        if (isSuperTenant.equals(0)) {
            wrapper.eq("tenant_id", user.getTenantId());
            Integer isHeadStore = user.getIsHeadStore();
            if (isHeadStore.equals(0)) {
                wrapper.eq("store_id", user.getStoreId());
            }
        }

        return wrapper;
    }



    @Override
    public void save(SysStoreDTO dto) {
        SysStoreEntity entity = ConvertUtils.sourceToTarget(dto, SysStoreEntity.class);
        if (entity.getStoreId() == null) {
            entity.setStoreId(snowflake.nextId()); // 主键id可能有错误
        }
        baseDao.insert(entity);

        // 批量插入客户端id和门店id的关联关系
        List<String> clientPrimaryIds = dto.getClientPrimaryIds();
        List<SysStoreClientsEntity> collect = clientPrimaryIds.stream().map(clientPrimaryId -> {
            return new SysStoreClientsEntity(entity.getStoreId(), clientPrimaryId);
        }).toList();
        baseDao.batchInsertClientIdStoreId(collect);
    }

    @Override
    public List<SysStoreEntity> selectStoreByTenantId(Long tenantId) {
        QueryWrapper<SysStoreEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tenant_id", tenantId);
        return baseDao.selectList(queryWrapper);
    }

    @Override
    public List<SysStoreEntity> undistributedlist(Long tenantId) {
        QueryWrapper<SysStoreEntity> queryWrapper = new QueryWrapper<>();
        if (tenantId != null) {
            queryWrapper.isNull("tenant_id").or().eq("tenant_id", tenantId);
        } else {
            queryWrapper.isNull("tenant_id");
        }

        return baseDao.selectList(queryWrapper);
    }

    @Override
    public List<SysStoreDTO> list(Map<String, Object> params) {
        InfoDetail user = SecurityUser.getUser();
        Integer isSuperTenant = user.getIsSuperTenant();
        if (isSuperTenant.equals(0)) {
            params.put("tenantId", user.getTenantId());
        }
        List<SysStoreEntity> sysStoreEntities = baseDao.selectList(getWrapper(params));
        return ConvertUtils.sourceToTarget(sysStoreEntities, SysStoreDTO.class);
    }



}